package org.geometerplus.fbreader.formats.txt;

import android.os.Handler;
import android.os.Looper;

import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.read.bean.Chapter;

import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.fbreader.FBView;
import org.geometerplus.fbreader.formats.BasicChapterReader;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.ZLTxtFile;
import org.geometerplus.zlibrary.text.view.ZLTextPosition;

import java.util.List;


/**
 * txt的readler
 */
public class MTxtReader extends BasicChapterReader {
    /**
     * 文件完整路径
     */
    protected String myFullPath;

    /**
     * 最后最后一章的内容
     */
    private long mLastDoLastChapterTime;


    public MTxtReader(FBReaderApp app, ZLFile file) {
        super(app, file);
    }

    @Override
    protected void init(ZLFile file) {
        myFullPath = file.getPath();
        myBookId = myReadingBook.getBookId();
        myBookName = myReadingBook.getName();
    }

//    protected void load(String fullPath) {
//        addChapters(new ChapterParser(fullPath).loadDir());
//    }

    public synchronized void addChapters(List<Chapter> chapters) {
        myChapters.addAll(chapters);
    }

    @Override
    protected ZLFile createZLFile(String bookId, String bookName, String chapterId, String chapterName) {
        return new ZLTxtFile(bookId, myBookName, chapterId, chapterName);
    }

    @Override
    public void gotoChapter(Chapter chapter, ZLTextPosition position) {
        ZLTxtFile file = new ZLTxtFile(myBookId, myBookName, chapter.getId(), chapter.getName());
        gotoChapter(file, position);
    }

    @Override
    public void gotoChapter(ZLFile zfile, ZLTextPosition position) {
        if (zfile == null) {
            if (myApplication != null) {
                final FBView fbView = (FBView) myApplication.getCurrentView();
                fbView.stopPendingLoadPageData();
                fbView.initErrorInfo("章节文件不存在");
            }
            return;
        }
        if (zfile.exists()) {
            ZLTxtFile mTxtFile = (ZLTxtFile) zfile;
            gotoChapterById(mTxtFile.getChapterId());
            super.gotoChapter(zfile, position);
        }
    }

    @Override
    public void readChapterSuccess(Chapter chapter) {
//        if (chapter != null) {
//            UploadData data = new UploadData();
//            data.setChapterid(chapter.getId());
//            data.setUploadtype(KConstants.READ_BOOK_TIME_TYPE);
//            data.setSaveTime(System.currentTimeMillis());
//            UploadManager.getsInstance().sendSaveDataMsg(data);
//        }
    }

    @Override
    public void doFirstChapter() {
        final FBReaderApp reader = (FBReaderApp) FBReaderApp.Instance();
        reader.BookTextView.toastFirstPage();
    }

    @Override
    public void doLastChapter() {
        // 处理快速翻页会多冷执行
//        long time = System.currentTimeMillis();
//        if (time - mLastDoLastChapterTime < 1000) {
//            return;
//        }
//        mLastDoLastChapterTime = time;
//        final FBReaderApp reader = (FBReaderApp) FBReaderApp.Instance();
//        new Handler(Looper.getMainLooper()).post(new Runnable() {
//            @Override
//            public void run() {
//                ActivityHelper.doJumpToWeb(reader.getAppContext(),
//                        UrlManager.getLocalBookRecommend());
//            }
//        });
    }


    @Override
    public void destory() {

    }
}
